package fmi.chesstern.pieces.strategy;

import java.util.HashSet;
import java.util.Set;


import fmi.chesstern.game.BoardField;
import fmi.chesstern.game.GameBoardState;
import fmi.chesstern.pieces.ChessPiece;
import fmi.chesstern.pieces.ChessPieceColor;

public class PawnStrategy implements ChessPieceStrategy {
	public Set<BoardField> getValidNewPositions(GameBoardState board, BoardField currentPosition) {
		Set<BoardField> result = new HashSet<BoardField>();
		
		ChessPieceColor color = board.getPieceAt(currentPosition).getColor();
		int direction = color == ChessPieceColor.WHITE ? 1 : -1;
		
		BoardField newPosition = currentPosition.offsetBy(direction, 0);
		
		if (StrategyUtilities.canPositionOn(board, newPosition, color)
				&& board.getPieceAt(newPosition) == null) {
			result.add(newPosition);
			
			if (!board.hasItBeenMoved(currentPosition)) {
				newPosition = currentPosition.offsetBy(direction * 2, 0);
				
				if (StrategyUtilities.canPositionOn(board, newPosition, color)
						&& board.getPieceAt(newPosition) == null) {
					result.add(newPosition);
				}
			}
		}
		
		for (int i : new int[] {-1, 1}) {
			newPosition = currentPosition.offsetBy(direction, i);
			
			if (StrategyUtilities.canPositionOn(board, newPosition, color)) {
				ChessPiece pieceAtNewPosition = board.getPieceAt(newPosition);
				
				if (pieceAtNewPosition != null && pieceAtNewPosition.getColor() != color) {
					result.add(newPosition);
				}
			}
		}
		
		return result;
	}
}
